package io.renren.wechat.controller;
/*
 *
 * Copyright(c) 1995-2020 by Asiainfo(China)
 * All rights reserved.
 *
 * 2020/12/12 14:24
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Id: RedisService.java
 * <p>
 * Copyright(c) 1995-2017 by Asiainfo.com(China)
 * All rights reserved.
 *
 * @author Jacob Liu <Liuxy8@asiainfo.com>
 * 2020/12/12 14:24
 */
@Component
public class RedisService {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisService.class);
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    public String setStringValue(String key, String value) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

        valueOperations.set(key, value);

        String valueInRedis = valueOperations.get(key);
        LOGGER.debug("got value from redis, value: [{}]", valueInRedis);

        return String.format("key=[%s], value=[%s]  set to redis successfully.", key, value);
    }


    public String getStringValue(String key) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

        String value = valueOperations.get(key);
        LOGGER.debug("get for key={}, value={}", key, value);

        if (null == value) {
            return String.format("key=[%s] not exists in redis.", key);
        }

        return String.format("key=[%s], value=[%s]", key, value);
    }


}
